//import 'package:barcode_scan/barcode_scan.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:qrscan/qrscan.dart' as scanner;

class ScanPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _ScanPageState();
  }
}

class _ScanPageState extends State<ScanPage> {
  String result;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('二维码扫描'),
      ),
      body: Column(
        crossAxisAlignment: CrossAxisAlignment.center,
        children: <Widget>[
          Container(
            color: Colors.yellowAccent,
            alignment: Alignment.center,
            child: Text(result ?? ''),
          ),
          Container(
            alignment: Alignment.center,
            child: FlatButton(
              onPressed: () async {
                _scan();
              },
              child: Text('barcode_scan开启扫描'),
            ),
          ),
          Container(
            alignment: Alignment.center,
            child: FlatButton(
              onPressed: () async {
                _qrcodeScan();
              },
              child: Text('qrcode_reader开启扫描'),
            ),
          ),
          Container(
            alignment: Alignment.center,
            child: FlatButton(
              onPressed: () async {
                String barcode = await scanner.scan();
                setState(() {
                  result = barcode;
                });
              },
              child: Text('qrscan'),
            ),
          ),

        ],
      ),
    );
  }

  Future _scan() async {
//    try {
//      var options = ScanOptions(
//        strings: {
//          'cancel': '取消',
//          "flash_on": "开启闪光",
//          "flash_off": "关闭闪光",
//        },
//      );
//      ScanResult scanResult = await BarcodeScanner.scan(options: options);
//      setState(() {
//        result = scanResult.rawContent;
//      });
//      print(scanResult.type);
//      print(scanResult.rawContent); // The barcode content
//      print(scanResult.format); // The barcode format (as enum)
//      print(scanResult.formatNote);
//    } on PlatformException catch (e) {
//      if (e.code == BarcodeScanner.cameraAccessDenied) {
//        ToastUtil.showToastCenter('缺少相机权限');
//      }
//    }
  }

  Future _qrcodeScan() async {
//    String string = await QRCodeReader()
//        .setAutoFocusIntervalInMs(200) // default 5000
//        .setForceAutoFocus(true) // default false
//        .setTorchEnabled(true) // default false
//        .setHandlePermissions(true) // default true
//        .setExecuteAfterPermissionGranted(true) // default true
//        .scan();
//    setState(() {
//      result = string;
//    });
  }
}
